package com.study.strategy.v2;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 策略模式的案例
 */
public class ComparatorTest {
    public static void main(String[] args) {
        Person[] people = new Person[]{new Person(10,159),new Person(12,178)};

        //这个new SortByAge() 就是具体的策略
        Arrays.sort(people ,new SortByAge());

    }
}

class Person{
    int age ;
    int height ;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public Person(int age, int height) {
        this.age = age;
        this.height = height;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", height=" + height +
                '}';
    }

}

//排序策略1 根据年龄排序
class SortByAge implements Comparator<Person>{
    @Override
    public int compare(Person o1, Person o2) {
        if(o1.getAge() > o2.getAge()){
            return 1;
        }else if(o1.getAge() < o2.getAge()){
            return -1;
        }
        return 0;
    }
}

//排序策略2 根据身高排序
class SortByHeight implements Comparator<Person>{
    @Override
    public int compare(Person o1, Person o2) {
        if(o1.getHeight() > o2.getHeight()){
            return 1;
        }else if(o1.getHeight() < o2.getHeight()){
            return -1;
        }
        return 0;
    }
}